package com.sh4.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sh4.dao.impl.XgfsBookDaoImpl;
import com.sh4.entity.XgfsBook;
import com.sh4.service.XgfsBookService;
import com.sh4.util.ContextHeader;
import com.sh4.util.Sh4Utils;
import com.sh4.wxservice.ClientService;

import net.sf.json.JSONObject;

@Service("xgfsBookServiceImpl")
@Transactional
public class XgfsBookServiceImpl implements XgfsBookService {

	@Autowired
	XgfsBookDaoImpl xgfsBookDaoImpl;

	@Override
	public void save(XgfsBook xb) {

		if (xb.getId() == null) {
			xb.setBookStatus("xgfs10000");// 表示全新预约
			xb.setCreateDate(new Date());


			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			String remark = "预约电话："+xb.getBookMobile() + ",预约地址："+xb.getBookAddress() + ",预约类型："+ xb.getBookType() +
					",预约面积：" + xb.getBookArea() + ",备注信息：" + xb.getRemark();
			
			ClientService.MSTemplate("oY_PQ0j2RVBIdO1FF9TYghGSTpP4", xb.getBookMobile(), sdf.format(new Date()), xb.getBookAddress(), remark, Sh4Utils.getAccess_token());
			ClientService.MSTemplate("oY_PQ0rwZtXZ_hhcR5BIAfIyAutc", xb.getBookMobile(), sdf.format(new Date()), xb.getBookAddress(), remark, Sh4Utils.getAccess_token());
			
			
		} else {
			xb.setUpdateBy(ContextHeader.getUserCode());
			xb.setUpdateDate(new Date());
		}

		xgfsBookDaoImpl.saveOrUpdate(xb);
	}

	@Override
	public List<Map<String, Object>> list(int pageSize, int pageNo, JSONObject params) {

		return xgfsBookDaoImpl.list(pageSize, pageNo, params);
	}

	@Override
	public void deleteXgfsBook(XgfsBook xb) {

		xgfsBookDaoImpl.delete(xb);

	}

	@Override
	public XgfsBook getXgfsBookById(String id) {

		return xgfsBookDaoImpl.getXgfsBookById(id);
	}

	@Override
	public int countXgfsBook(JSONObject params) {

		return xgfsBookDaoImpl.countXgfsBook(params);
	}

	@Override
	public void cancle(XgfsBook xb) {
		xgfsBookDaoImpl.executeUpdateBySql(
				"update xgfs_book xb set xb.BOOK_STATUS_ = ? ,xb.UPDATE_DATE_ = ? , xb.UPDATE_BY_ = ? where xb.ID_ = ?",
				"xgfs10003", new Date(), ContextHeader.getUserCode(), xb.getId());

	}

}
